
package com.dy.template;

import java.io.IOException;
import java.util.List;

import com.dy.model.Column;
import com.dy.model.Creator;
import com.dy.model.TableInfo;

import cn.dy.util.FileUtil;

/**
 * @Description:
 * @Date：2018年6月28日 @author： DongY
 * @version ：1.0 1.[2018年6月28日]CreatBy：dy
 */

public class HtmlAddPage implements BaseSting {
	public static int numInRow = 2;

	@Override
	public String getStr(final TableInfo tableInfo, final Creator c) {
		String htm = "";
		try {
			htm = FileUtil.readAsString(c.getTemplatePath());
			htm = htm.replace("###formInfo###", getForm(tableInfo));
		} catch (final IOException e) {
			e.printStackTrace();
			System.err.println(e);
		}
		return htm;
	}

	public static boolean isSkip(final Column column) {
		final boolean flg = false;
		if ("id".equals(column.getJavaName()) || column.isPRI()) {
			return true;
		}
		return flg;
	}

	public String getForm(final TableInfo tableInfo) {
		String string = "\r\n\t\t<form class='layui-form' action='' lay-filter='form-edit'>";
		final List<Column> columns = tableInfo.getColumns();
		int i = 0;
		int j = 0;
		string += "\r\n\t\t\t<input type='hidden' name='" + tableInfo.getPriKeyColumn() + "'  data-link='{:"
				+ tableInfo.getPriKeyColumn() + "}' />";
		for (final Column column : columns) {
			j++;
			if (isSkip(column)) {
				continue;
			}
			if (i % numInRow == 0) {
				string += "\r\n\t\t\t<div class='layui-form-item'>";
			}
			string += "\r\n\t\t\t\t<div class='layui-inline'>";
			string += "\r\n\t\t\t\t\t<label class='layui-form-label'>" + column.getRemark() + "</label>";
			string += "\r\n\t\t\t\t\t<div class='layui-input-inline'>";
			string += "\r\n\t\t\t\t\t\t<input type='text' placeholder=''data-link='{:" + column.getJavaName()
					+ "}' id='_" + column.getJavaName() + "' name='" + column.getJavaName() + "' class='layui-input'>";
			string += "\r\n\t\t\t\t\t</div>\r\n\t\t\t\t</div>";
			if (i % numInRow == numInRow - 1 || j == columns.size()) {
				string += "\r\n\t\t\t</div>";
			}
			i++;
		}
		string += "\r\n\t\t</form>";
		return string;
	}

	// ------------------------------改由模板读取，下面方法弃用
	@Deprecated
	public static String getBody(final TableInfo tableInfo) {
		String string = "\r\n<body>\r\n<div class='kit-table'>\r\n\t<div style='margin:10px'>"
				+ "\r\n\t\t<form class='layui-form' action='' lay-filter='form-edit'>";
		final List<Column> columns = tableInfo.getColumns();
		int i = 0;
		int j = 0;
		string += "\r\n\t\t\t<input type='hidden' name='" + tableInfo.getPriKeyColumn() + "'  data-link='{:"
				+ tableInfo.getPriKeyColumn() + "}' />";
		for (final Column column : columns) {
			j++;
			if (isSkip(column)) {
				continue;
			}
			if (i % numInRow == 0) {
				string += "\r\n\t\t\t<div class='layui-form-item'>";
			}
			string += "\r\n\t\t\t\t<div class='layui-inline'>";
			string += "\r\n\t\t\t\t\t<label class='layui-form-label'>" + column.getRemark() + "</label>";
			string += "\r\n\t\t\t\t\t<div class='layui-input-inline'>";
			string += "\r\n\t\t\t\t\t\t<input type='text' placeholder=''data-link='{:" + column.getJavaName()
					+ "}' id='$" + column.getJavaName() + "' name='" + column.getJavaName() + "' class='layui-input'>";
			string += "\r\n\t\t\t\t\t</div>\r\n\t\t\t\t</div>";
			if (i % numInRow == numInRow - 1 || j == columns.size()) {
				string += "\r\n\t\t\t</div>";
			}
			i++;
		}
		string += "\r\n\t\t</form>\r\n\t</div>\r\n</div>\r\n";
		string += getJs() + "\r\n</body>";
		return string;
	}

	@Deprecated
	public static String getHread(final String title) {
		String string = "<!DOCTYPE html>\r\n<html>\r\n\t<head>\r\n\t\t<meta charset=\"UTF-8\">";
		string += "\r\n\t\t<title>" + title + "</title>\r\n\t\t";
		string += "<link rel=\"stylesheet\" type=\"text/css\" href=\"../../plugins/layui/css/layui.css\" />\r\n\t\t";
		string += "<link href=\"../../kitAdmin/css/app.css\" rel=\"stylesheet\" type=\"text/css\" />";
		return string + "\r\n</head>";
	}

	@Deprecated
	public static String getEnd() {
		final String string = "\r\n</html>";
		return string;
	}

	@Deprecated
	public static String getJs() {
		String js = "\r\n\t<script src=\"../../common/js/jquery.js\" type=\"text/javascript\" charset=\"utf-8\"></script>";
		js += "\r\n\t<script src=\"https://cdn.bootcss.com/jsviews/0.9.90/jsviews.js\" type=\"text/javascript\"></script>";
		return js;
	}

}
